<template>
	<div style="margin: 20px;">
                 
        <div>
            <ul>
                <li>
                    <Button type="primary" icon="plus-round" @click="openNewModal()">新建</Button>
                    <Button type="success" icon="wrench" @click="openModifyModal()">修改</Button>
                    <Button type="error" icon="trash-a" @click="del()">删除</Button>
                </li>
                <li>
                    <div style="padding: 10px 0;">
                    	<Table border :columns="columns1" :data="data1" :height="300" @on-selection-change="s=>{change(s)}" @on-row-dblclick="s=>{dblclick(s)}"></Table>                    
                    </div> 
                    
                </li>
                <li>
                    <div style="text-align: right;">
                        <Page :total="total" :page-size="pageInfo.pageSize" show-elevator show-total @on-change="e=>{pageSearch(e)}"></Page>
                    </div>  
                </li>
            </ul>
        </div>
        <!--添加modal-->  
        <Modal :mask-closable="false" :visible.sync="newModal" :loading = "loading" v-model="newModal" width="600" title="新建" @on-ok="newOk('menuNew')" @on-cancel="cancel()">
            <Form ref="menuNew" :model="menuNew" :rules="ruleNew" :label-width="80" >
                <Row>
                    <Col span="12">
                        <Form-item label="团队名称:" prop="team">
                            <Input v-model="menuNew.team" style="width: 204px"/>
                        </Form-item>
                    </Col>
                    <Col span="12">
                        <Form-item label="团队人数:" prop="num">
                            <Input v-model="menuNew.num" style="width: 204px"/>
                        </Form-item>
                    </Col>
                </Row>
                <Row>
                    <Col span="12">
                        <Form-item label="工作时间" prop="time">
                            <Input v-model="menuNew.time" style="width: 204px"/>
                        </Form-item>
                    </Col>
                    <Col span="12">
                        <Form-item label="启动资金" prop="funds">
                            <Input v-model="menuNew.funds" style="width: 204px"/>
                        </Form-item>
                    </Col>
                </Row>
               
               
                
            </Form>
        </Modal>
        <!--修改modal-->  
        <Modal :mask-closable="false" :visible.sync="modifyModal" :loading = "loading" v-model="modifyModal" width="600" title="修改" @on-ok="modifyOk('xiugaimenu')" @on-cancel="cancel()">
            <Form ref="xiugaimenu" :model="xiugaimenu" :rules="ruleModify" :label-width="80" >
                <Row>
                    <Col span="12">
                       <Form-item label="团队名称:" prop="team">
                            <Input v-model="xiugaimenu.team" style="width: 204px"/>
                        </Form-item>
                    </Col>
                    <Col span="12">
                       <Form-item label="团队人数:" prop="num">
                            <Input v-model="xiugaimenu.num" style="width: 204px"/>
                        </Form-item>
                    </Col>
                </Row>
                <Row>
                     <Col span="12">
                        <Form-item label="工作时间" prop="time">
                            <Input v-model="xiugaimenu.time" style="width: 204px"/>
                        </Form-item>
                    </Col>
                    <Col span="12">
                        <Form-item label="启动资金" prop="funds">
                            <Input v-model="xiugaimenu.funds" style="width: 204px"/>
                        </Form-item>
                    </Col>
                </Row>
                
               
            </Form>
        </Modal>
    </div>
</template>
<script>
	export default {
        data () {
            return {
                /*用于查找的菜单id*/
                id:null,
            	/*选择的数量*/
                count:null,
            	/*选中的组数据*/
                groupId:null,
            	/*新建modal的显示参数*/
                newModal:false,
                /*修改modal的显示参数*/
                modifyModal:false,
            	/*分页total属性绑定值*/
                total:0,
                /*loading*/
                loading: true,
                /*pageInfo实体*/
                pageInfo:{
                	page:0,
                	pageSize:10
                },
                /*menu实体*/
                menu:{                             
                    id:null,
                    team:null,
                    num:null,
                    time:null,
                    funds:null
                  
                },
                 xiugaimenu:{                             
                    id:null,
                    team:null,
                    num:null,
                    time:null,
                    funds:null
                  
                },
                /*用于添加的menu实体*/
                menuNew:{
                	  id:null,
                    team:null,
                    num:null,
                    time:null,
                    funds:null
                },
                /*用于修改的menu实体*/
                menuModify:{
                	  id:null,
                    team:null,
                    num:null,
                    time:null,
                    funds:null
                },
                /*新建验证*/
                ruleNew:{
                    team: [
                        { type:'string',required: true, message: '输入团队名', trigger: 'blur' }
                    ],
                    num: [
                        { type:'string',required: true, message: '输入人数', trigger: 'blur' }
                    ],
                      time: [
                        { type:'string',required: true, message: '输入时间', trigger: 'blur' }
                    ],
                      funds: [
                        { type:'string',required: true, message: '输入资金数目', trigger: 'blur' }
                    ],
                       id: [
                        { type:'int',required: true, message: '输入id', trigger: 'blur' }
                    ],
                   
                   
                },
                /*修改验证*/
                ruleModify:{
                     team: [
                        { type:'string',required: true, message: '输入团队名', trigger: 'blur' }
                    ],
                    num: [
                        { type:'string',required: true, message: '输入人数', trigger: 'blur' }
                    ],
                      time: [
                        { type:'string',required: true, message: '输入时间', trigger: 'blur' }
                    ],
                      funds: [
                        { type:'string',required: true, message: '输入资金数目', trigger: 'blur' }
                    ],
                     id: [
                        { type:'int',required: true, message: '输入id', trigger: 'blur' }
                    ],
                   
                },
                /*菜单列表*/
                menuList:[],
            	/*生产类型表显示字段*/
            	columns1: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '团队编号',
                        key: 'id'
                    },
                    {
                        title: '团队名称',
                        key: 'team'
                    },
                    {
                        title: '团队人数',
                        key: 'num'
                    },
                    {
                        title: '工作时间',
                        key: 'time'
                    },
                    {
                        title: '启动资金',
                        key: 'funds'
                    }
                   
                ],
                /*生产类型表数据*/
                data1: []
            }
        },
        mounted(){
        	/*页面初始化调用方法*/
            this.getTable({
                "pageInfo":this.pageInfo,
                'id':this.id
            });
            this.axios({
              method: 'get',
              url: '/menus/parentId',
              params: {
                'parentId': 0 
              }
            }).then(function (response) {
                var listTemp = response.data;
                for (var i = 0; i < listTemp.length; i++) {
                    this.menuList.push({
                        "value": listTemp[i].id,
                        "label": listTemp[i].name
                    });
                }
            }.bind(this)).catch(function (error) {
              alert(error);
            });
        },
        methods:{
        	/*pageInfo实体初始化*/
        	initPageInfo(){
        		this.pageInfo.page = 0;
        		this.pageInfo.pageSize = 10;
        	},
            /*menu实体初始化*/
            initMenu(){
                this.menu.id = null;
                this.menu.team = null;
                this.menu.num = null;
                this.menu.time = null;
                this.menu.funds = null;
                
            },

             /*menu实体初始化*/
            xiugai(e){
                this.xiugaimenu.id = e.id;
                this.xiugaimenu.team = e.team;
                this.xiugaimenu.num = e.num;
                this.xiugaimenu.time = e.time;
                this.xiugaimenu.funds = e.funds;
                
            },
            /*menuNew实体初始化*/
            initMenuNew(){
                this.menuNew.id = null;
                this.menuNew.team = null;
                this.menuNew.num = null;
                this.menuNew.time = null;
                this.menuNew.funds = null;
               
            },
            /*menuModify实体初始化*/
            initMenuModify(){
                this.menuModify.id = null;
                this.menuModify.team = null;
                this.menuModify.num = null;
                this.menuModify.time = null;
                this.menuModify.funds = null;
           
            },
            /*menuNew设置*/
            menuSet(e){
                this.menu.id = e.id;
                this.menu.team = e.team;
                this.menu.num = e.num;
                this.menu.time = e.time;
                this.menu.funds = e.funds;
         
            },
            /*menuNew设置*/
            menuNewSet(e){
                this.menuNew.id = e.id;
                this.menuNew.team = e.team;
                this.menuNew.num = e.num;
                this.menuNew.time = e.time;
                this.menuNew.funds = e.funds;
              
            },
            /*menuModify设置*/
            menuModifySet(e){
               
                this.menuModify.id = e.id;
                this.menuModify.team = e.team;
                this.menuModify.num = e.num;
                this.menuModify.time = e.time+'';
                this.menuModify.funds = e.funds+'';
            
            },
            /*得到表数据*/
            getTable(e) {
                this.axios({
                  method: 'get',
                  url: '/lqras',
                  params: {
                    'page':e.pageInfo.page,
                    'pageSize':e.pageInfo.pageSize,
                    'id':e.id
                  }
                }).then(function (response) {
                    this.data1=response.data.data;
                    this.total=response.data.totalCount;
                }.bind(this)).catch(function (error) {
                  alert(error);
                });
            },
            /*搜索按钮点击事件*/
            search(){
                this.initPageInfo();
                this.getTable({
                    "pageInfo":this.pageInfo,
                    'id':this.id
                });   
            },
            /*分页点击事件*/
            pageSearch(e){
                this.pageInfo.page = e-1;
                this.getTable({  
                    "pageInfo":this.pageInfo,
                    'id':this.id
                });
            },
            /*新建点击触发事件*/
            openNewModal(){
                this.newModal = true;
                this.initMenuNew();
                this.data1.sort();
                this.count = 0;
                this.groupId = null;
            },
            /*新建modal的newOk点击事件*/
            newOk (menuNew) { 
                this.$refs[menuNew].validate((valid) => {
                    if (valid) {
                        this.initMenu();
                        this.menuSet(this.menuNew);
                        this.axios({
                            method: 'post',
                            url: '/insertlqra',
                            data: this.menu
                        }).then(function (response) {
                            this.initMenuNew();
                            this.getTable({
                                "pageInfo":this.pageInfo,
                                'id':this.id
                            });
                            this.$Message.info('新建成功');
                        }.bind(this)).catch(function (error) {
                            alert(error);
                        });  
                        this.newModal = false;
                    }else {
                        setTimeout(() => {
                            this.loading = false;
                            this.$nextTick(() => {
                                this.loading = true;
                            });
                        }, 1000);
                    }
                })
            },
            /*点击修改时判断是否只选择一个*/
            openModifyModal(e){
             //   console.log(this.groupId)
                
                if(this.count > 1 || this.count < 1){
                    this.modifyModal= false; 
                    this.$Message.warning('请至少选择一项(且只能选择一项)');  
                }else{
                    this.modifyModal = true;

                    this.axios({
                        method: 'post',
                        url: '/getByIda',
                        data: this.groupId
                  
                }).then(function (response) {
                  
                    this.xiugai(response.data)
                   
                  //menuModifySet(response)
                }.bind(this)).catch(function (error) {
                  alert(error);
                });
                }
            },
            /*修改modal的modifyOk点击事件*/
             modifyOk(xiugaimenu) { 
                this.$refs[xiugaimenu].validate((valid) => {
                    
                    if (valid) {
                        this.initMenu();
                        this.menuSet(this.xiugaimenu);
                        this.axios({
                          method: 'put',
                          url: '/updatelqra',
                          data: this.menu
                        }).then(function (response) {
                            this.initMenuNew();
                            this.getTable({
                                "pageInfo":this.pageInfo,
                                'id':this.id
                            });
                            this.$Message.info('修改成功');
                        }.bind(this)).catch(function (error) {
                          alert(error);
                        });  
                        this.modifyModal = false;
                    }else {
                        this.$Message.error('表单验证失败!');
                        setTimeout(() => {
                            this.loading = false;
                            this.$nextTick(() => {
                                this.loading = true;
                            });
                        }, 1000);
                    }
                })
            },
            /*modal的cancel点击事件*/
            cancel () {
                this.$Message.info('点击了取消');
            },
            /*table选择后触发事件*/
            change(e){
                if(e.length==1){
                    this.menuModifySet(e['0']);
                }
                this.setGroupId(e);  
                    
            },
            /*通过选中的行设置groupId的值*/
            setGroupId(e){
                this.groupId=[];
                this.count=e.length;
                for (var i = 0; i <= e.length - 1; i++) {
                    this.groupId.push(e[i].id);
                }
            },
            /*删除table中选中的数据*/
            del(){
                if(this.groupId!=null && this.groupId!=""){
                    this.axios({
                      method: 'delete',
                      url: '/deletelqra',
                      data: this.groupId
                    }).then(function (response) {
                        this.getTable({
                            "pageInfo":this.pageInfo,
                            'id':this.id
                        });
                        this.groupId=null;
                        this.count=0;
                        this.$Message.info('删除成功');
                    }.bind(this)).catch(function (error) {
                        alert(error);
                    });
                }
            },
            /*表格中双击事件*/
            dblclick(e){
                this.menuModifySet(e);
                this.modifyModal = true;
                this.data1.sort();
            }
        }
    }
</script>